package com.yay.danceline.util;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;


public class Media {
	private static MediaPlayer mp;
	
	public static MediaPlayer getMediaPlayer() {
		if(mp == null) {
			mp = new MediaPlayer();
		}
		return mp;
	}
	
	public static void play(final String url, final OnCompletionListener listener) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					try {
						getMediaPlayer().setDataSource(url);
					} catch (IllegalStateException e) {
						getMediaPlayer().release();
						mp = new MediaPlayer();
						mp.setOnCompletionListener(listener);
						getMediaPlayer().setDataSource(url);
					}
					getMediaPlayer().prepare();
					getMediaPlayer().start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
	
}
